package com.huatai.bpmn.commom.util;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.alibaba.fastjson.JSONObject;
import com.huatai.bpmn.entity.dto.WeChatDeptDto;
import com.huatai.user.dto.WeChatPeopleDto;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 企业微信接口对接
 * @author :yxy
 * @date : 2023/8/28
 */
public class WechatUtil {

	private static String corpid="ww268821502cc7f56c";
	private static String corpsecret="iCAryTo9ccYOnE5N2SMISOOqlf2yUqjUoEl540wXdfY";

	/**
	 * 获取企业token
	 * @return
	 */
	public static String getToken(){
		String url="https://qyapi.weixin.qq.com/cgi-bin/gettoken";
		HashMap<String, String> map = new HashMap<>();
//		map.put("corpid","ww268821502cc7f56c");
		map.put("corpid",corpid);
//		map.put("corpsecret","iCAryTo9ccYOnE5N2SMISOOqlf2yUqjUoEl540wXdfY");
		map.put("corpsecret",corpsecret);
		String msg=null;
		try {
			msg = HttpsUtils.sendGet(url, null, map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		String token = JSONObject.parseObject(msg).getOrDefault("access_token", "").toString();
		return token;
	}

	/**
	 * 获取部门列表
	 * @return
	 */
	public static List<WeChatDeptDto> getDeptList(){
		String token = getToken();
		HashMap<String, String> map = new HashMap<>();
		map.put("access_token", token);
		map.put("id", "");
		String url = "https://qyapi.weixin.qq.com/cgi-bin/department/list";
		String deptList = null;
		try {
			deptList = HttpsUtils.sendGet(url, null, map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<WeChatDeptDto> list = JSONObject.parseObject(deptList).getJSONArray("department").toJavaList(WeChatDeptDto.class);
		if (list.size()==0){
			throw new RuntimeException("可信ip可能失效，请联系管理员");
		}
		return list;
	}

	/**
	 * 获取部门人员详情
	 * @param departmentId
	 * @return
	 */
	public static List<WeChatPeopleDto> getDeptPeopleList(String departmentId, String departmentName){
		String token = getToken();
		HashMap<String, String> map = new HashMap<>();
		map.put("access_token", token);
//		map.put("department_id", "14");
		map.put("department_id", departmentId);
		String url = "https://qyapi.weixin.qq.com/cgi-bin/user/list";
		String deptList = null;
		try {
			deptList = HttpsUtils.sendGet(url, null, map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<WeChatPeopleDto> list = JSONObject.parseObject(deptList).getJSONArray("userlist").toJavaList(WeChatPeopleDto.class);
		list.forEach(x->x.setDepartName(departmentName));
		return list;
	}

	/**
	 * 获取人员信息
	 * @return
	 */
	public static Set<WeChatPeopleDto> getList(){
		Set<WeChatPeopleDto> userSet = new TreeSet<>(Comparator.comparing(WeChatPeopleDto::getUserid));
		List<WeChatDeptDto> deptList = getDeptList();

		deptList.forEach(x->{
			List<WeChatPeopleDto> deptPeopleList = getDeptPeopleList(x.getId(),x.getName());
			userSet.addAll(deptPeopleList);
		});
		return userSet;
	}


	public static void main(String[] args) {
		/*Set<WeChatPeopleDto> list = getList();
		System.out.println(list.size());*/

	}
}
